package util.cleaner;

import java.util.HashSet;
import java.util.Hashtable;

import util.io.FileInput;

public class GenerateGraphIds {

	/**
	 * @param args
	 */
	
	static private int _id_url = 0;
	static private int _id_tag = 1;
	static private int _url = 2;
	static private int _tag = 3;
	static private int _freq_u = 5;
	static private int _freq_tag = 4;
	static private int _freq_ut = 6;
	static private int _age = 7;
	
	int offset= 10000000;
	
	public static void generateId(String path){
		
		FileInput in = new FileInput(path);
		String line= in.readString();
		int ids=0;
		
		
		
		Hashtable<String,Integer> urls_h = new Hashtable<String,Integer>();
		
		Hashtable<String,Integer> tags_h = new Hashtable<String,Integer>();
		
		//add url ids
		
		while(line!=null){

			String[] fields = line.split("\t");

			Integer id_url = Integer.valueOf(Integer.valueOf(fields[_id_url]));
			Integer id_tag = Integer.valueOf(Integer.valueOf(fields[_id_tag]));

			Integer freq_tag = Integer.valueOf(Integer.valueOf(fields[_freq_tag]));
			Integer freq_url = Integer.valueOf(Integer.valueOf(fields[_freq_u]));
			Integer freq_ut = Integer.valueOf(Integer.valueOf(fields[_freq_ut]));
			String age = fields[_age];
			String name = fields[_tag];
			String url_name = fields[_url];
			
			if(!urls_h.containsKey(url_name)){
				
				
				urls_h.put(url_name, ids);
				ids++;
				
			}
			
			
			
			
			line = in.readString();
		}
		
		
		
		//add tag ids
		ids = ids + 100000;
		in = new FileInput(path);
		line= in.readString();
		
		while(line!=null){

			String[] fields = line.split("\t");

			Integer id_url = Integer.valueOf(Integer.valueOf(fields[_id_url]));
			Integer id_tag = Integer.valueOf(Integer.valueOf(fields[_id_tag]));

			Integer freq_tag = Integer.valueOf(Integer.valueOf(fields[_freq_tag]));
			Integer freq_url = Integer.valueOf(Integer.valueOf(fields[_freq_u]));
			Integer freq_ut = Integer.valueOf(Integer.valueOf(fields[_freq_ut]));
			String age = fields[_age];
			String name = fields[_tag];
			String url_name = fields[_url];
			
		
			
			
			if(!tags_h.containsKey(name)){
				
				tags_h.put(name, ids);
				
				ids++;
				
			}
			
			
			line = in.readString();
		}
		
		
		//regerate file
		
		in = new FileInput(path);
		line= in.readString();
	
		while(line!=null){

			String[] fields = line.split("\t");

			Integer id_url = Integer.valueOf(Integer.valueOf(fields[_id_url]));
			Integer id_tag = Integer.valueOf(Integer.valueOf(fields[_id_tag]));

			Integer freq_tag = Integer.valueOf(Integer.valueOf(fields[_freq_tag]));
			Integer freq_url = Integer.valueOf(Integer.valueOf(fields[_freq_u]));
			Integer freq_ut = Integer.valueOf(Integer.valueOf(fields[_freq_ut]));
			String age = fields[_age];
			String name = fields[_tag];
			String url_name = fields[_url];
			
			
			int url_id_new= urls_h.get(url_name);
			int tag_id_new = tags_h.get(name);
			
			System.out.println(url_id_new + "\t"+ tag_id_new + "\t"+ url_name + "\t"+ name +"\t"
					+freq_tag + "\t"+ freq_url + "\t"+ freq_ut + "\t"+ age);
			
			line=in.readString();
			
			
		}
		
		
		
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String prefix = "/Users/sergioduarte/projects/data/graph/";
		
		//String path = "/media/sata__/data/delicious_graph/delicious_graphML_raw_ids_dmoz.txt";
		String path = prefix+ "delicious_graphML_raw_ids_dmoz.txt";
		
		generateId(path);

	}

}
